<?php
class Zeus
{

	/**
	 * Registry array
	 * 
	 * @var array
	 */
	public static $_registry;

	/**
	 * Modules information
	 */
	public static $_modules;


	/**
	 * Initializes some essentials
	 */
	public static function initialize()
	{
		// Load simple core functions
		self::loadLibraryFile('weltherrschaft/functions');
		self::loadLibraryFile('zeusLib/object');

		// Load the zeus libraray
		self::loadLibrary('zeusLib');
		self::loadLibrary('weltherrschaft');

		// Initialize variables
		self::$_registry = new ZeusLib_Object();
		self::$_modules  = new ZeusLib_Object();

		// Load and initialize some stuff
		Config::load();
		Language::load();
		RewriteRule::initialize();

		// Load modules
		self::loadAllModules();

		// Check program rights
		// TODO: überlegen wie
	}

	/**
	 * Loads all modules
	 */
	private static function loadAllModules()
	{
		// TODO
	}

	/**
	 * Run Zeus
	 */
	public static function run()
	{
		// TODO
	} 

	/**
	 * Loads all libraries from a package that were not loaded yet
	 * 
	 * @param string $index
	 */
	public static function loadLibrary($index)
	{
		// Get the library or file path
		$path = './libraries/'.self::_namespaceToPath($index, false, false);

		// Scan the dir
		$scan = scan_dir($path);

		// Iterate the dir
		foreach($scan as $result)
		{
			if(is_php_file($path.'/'.$result))
			{
				// Include library
				include_once($path.'/'.$result);
			}
			elseif(is_dir($path.'/'.$result))
			{
				self::loadLibrary($index.'_'.$result);
			}
		}
	}

	/**
	 * Loads the library file if it was not loaded yet
	 * 
	 * @param string $index
	 */
	public static function loadLibraryFile($index)
	{
		// Get the library path
		$path = './libraries/'.self::_namespaceToPath($index);

		// Include library
		include_once($path);
	}

	/**
	 * Puts the value by the given key into the registry
	 * 
	 * @param string $key
	 * @param mixed $value
	 */
	public static function register($key, $value)
	{
		// Add the data
		$this->_registry->addData($key, $value);
	}

	/**
	* Gets the data by the given key from the registry
	*
	* @param string $key
	*/
	public static function registry($key)
	{
		// Return the data
		return $this->_registry->getData($key);
	}

	/**
	 * Deletes the value by the given key from the registry
	 * 
	 * @param string $key
	 */
	public static function unregister($key)
	{
		// Delete the data
		$this->_registry->unsetData($key);
	}


	/**
	 * Converts a namespace into a path
	 * 
	 * @param String $namespace
	 * @param boolean $replace
	 * @param boolean $appendPhp
	 * @return string
	 */
	private static function _namespaceToPath($namespace, $replace = false, $appendPhp = true)
	{
		// Check whether we want to replace
		if($replace)
		{
			// Replace slashes with underscores
			$path = str_replace('/', '_'.$replace.'_', $namespace);
		}
		// We don't want to replace
		else
		{
			// Replace slashes with spaces
			$path = str_replace('/', ' ', $namespace);
		}
		// Make the words uppercade
		$path = str_replace('_', ' ', $path);
		$path = ucwords($path);
		$path = str_replace('_', '/', $path);
		$path = str_replace(' ', '/', $path);

		// Check whether we want to add the php mime type
		if($appendPhp)
		{
			// Add .php
			return $path.'.php';
		}

		// Return the result
		return $path;
	}

}